//
//  CJImagesTableViewController2.m
//  CJFoundationDemo
//
//  Created by ciyouzen on 2016/3/26.
//  Copyright © 2016年 dvlproad. All rights reserved.
//

#import "CJImagesTableViewController2.h"
#import <SDWebImage/SDWebImage.h>
#import "CQTSNetImagesUtil.h"
#import "CQTSLocImagesUtil.h"
#import "CJImagesTableViewCell1.h"

@interface CJImagesTableViewController2 () <UITableViewDataSource, UITableViewDelegate> {
    
}

@end

@implementation CJImagesTableViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(@"一定要加载大图列表的时候(查看重用效果)", nil); //知识点:使得tabBar中的title可以和显示在顶部的title保持各自
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //[tableView registerClass:[CJImagesTableViewCell1 class] forCellReuseIdentifier:@"cell"];
    tableView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]; // #f5f5f5
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(20);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(4*150);
    }];
//    tableView.estimatedRowHeight = 100;
//    tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView = tableView;
    
    
    NSMutableArray *sectionDataModels = [[NSMutableArray alloc] init];
    
    //ImagesTable
    {
        CQDMSectionDataModel *sectionDataModel = [[CQDMSectionDataModel alloc] init];
        sectionDataModel.theme = @"ImagesTable";
        NSMutableArray<CQTSNetImageDataModel *> *imageDataModels = [CQTSNetImagesUtil __getTestNetImageDataModelsWithCount:100 randomOrder:NO];
        [sectionDataModel.values addObjectsFromArray:imageDataModels];
        
        [sectionDataModels addObject:sectionDataModel];
    }
    
    self.sectionDataModels = sectionDataModels;
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionDataModels.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CQDMSectionDataModel *sectionDataModel = [self.sectionDataModels objectAtIndex:section];
    NSArray *dataModels = sectionDataModel.values;
    
    return dataModels.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CQDMSectionDataModel *sectionDataModel = [self.sectionDataModels objectAtIndex:indexPath.section];
    NSArray *dataModels = sectionDataModel.values;
    CQTSNetImageDataModel *moduleModel = [dataModels objectAtIndex:indexPath.row];
    
    CJImagesTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[CJImagesTableViewCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.titleNameLabel.text = [NSString stringWithFormat:@"%ld-%ld", indexPath.section, indexPath.row];
    cell.messageTipLabel.text = moduleModel.name;
    
    NSURL *imageURL = [NSURL URLWithString:moduleModel.imageUrl];
//    UIImage *placeholderImage = [CQTSLocImagesUtil cjts_placeholderImage01];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [cell.iconImageView sd_setImageWithURL:imageURL placeholderImage:placeholderImage];
//    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

        dispatch_async(dispatch_get_main_queue(), ^{
            cell.iconImageView.image = image;
        });
        
    });
     
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"didSelectRowAtIndexPath = %zd %zd", indexPath.section, indexPath.row);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CQDMSectionDataModel *sectionDataModel = [self.sectionDataModels objectAtIndex:indexPath.section];
    NSArray *dataModels = sectionDataModel.values;
    CQTSNetImageDataModel *moduleModel = [dataModels objectAtIndex:indexPath.row];
    
    [self execModuleModel:moduleModel];
}


- (void)execModuleModel:(CQTSNetImageDataModel *)moduleModel {
    
}
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
